package com.zhiyou100.crm.servlet.notice;

import java.io.IOException;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.zhiyou100.crm.service.NoticeService;
import com.zhiyou100.crm.service.impl.NoticeServiceImpl;
import com.zhiyou100.crm.util.AdminBaseServlet;
import com.zhiyou100.crm.util.Notice;
import com.zhiyou100.crm.util.User;

/**
 * 发布公告
 */
@WebServlet("/notice/add")
public class AddServlet extends AdminBaseServlet {

	private static final long serialVersionUID = 1L;
	NoticeService noticeService=new NoticeServiceImpl();

       
	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html; charset=UTF-8");
		User user = (User) request.getSession().getAttribute("user");
		String pubTime = request.getParameter("pubTime");
		String receiveId = request.getParameter("receiveId");
		String expireTime = request.getParameter("expireTime");
		String subject = request.getParameter("subject");
		String text = request.getParameter("text");
		SimpleDateFormat format2 = new SimpleDateFormat("yyyy-mm-dd");
		format2.setLenient(false);
		try {
			if(pubTime!=null&&receiveId!=null) {
				Timestamp pubTime1 = new Timestamp(format2.parse(pubTime).getTime());
				Timestamp expireTime1 = new Timestamp(format2.parse(expireTime).getTime());
//				Timestamp pubTime1= Timestamp.valueOf(pubTime);
//				Timestamp expireTime1 = Timestamp.valueOf(expireTime);
				Notice notice = new Notice(subject, text, expireTime1,receiveId,pubTime1);
				int a = noticeService.addNotice(notice,user);
				if (a > 0) {
					response.sendRedirect(request.getContextPath() + "/notice/list");
				} else {
					request.getRequestDispatcher("/WEB-INF/view/notice/add.jsp").forward(request, response);
				}
			}else {
				request.getRequestDispatcher("/WEB-INF/view/notice/add.jsp").forward(request, response);
			}

		} catch (ParseException e) {
			e.printStackTrace();
		}


	}

	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request,response);
	}

}
